<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
    :append-to-body="true"
  >
    <el-row>
      <el-form
        label-width="80px"
        ref="createForm"
        :rules="rules"
        :model="formData"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.displayName" />
        </el-form-item>
        <el-form-item label="角色代码" prop="displayName">
          <el-input v-model="formData.name" />
        </el-form-item>
        <!--  <el-form-item label="是否锁定">
          <el-tooltip
            class="item"
            effect="dark"
            content="锁定后不能删除"
            placement="right"
          >
            <el-checkbox v-model="formData.isStatic"></el-checkbox>
          </el-tooltip>
        </el-form-item>
       <el-form-item label="权限">
          <el-checkbox
            v-for="(item, index) in permissionsData"
            :key="index"
            disabled
            checked=""
          >
            {{ item.displayName }}
          </el-checkbox>
        </el-form-item> -->
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose()">取 消</el-button>
      <el-button type="primary" @click="comit()" :loading="loading"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { GetAllPermissions, Create, Update } from '@api/systemManage/roleManage'
export default {
  data () {
    return {
      title: '',
      dialogVisible: false,
      permissionsData: [],
      loading: false,
      formData: {
        description: '',
        displayName: '',
        id: 0,
        isStatic: false,
        name: '',
        normalizedName: '',
        permissions: []
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        displayName: [
          { required: true, message: '请输入角色代码', trigger: 'blur' }
        ]
      }

    }
  },
  created () {
    GetAllPermissions().then(res => {
      if (res.success) {
        this.permissionsData = res.result.items
      }
    })
  },
  methods: {
    comit () {
      this.$refs.createForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.title === '新增') {
            this.createRole()
          } else {
            this.updateRole()
          }
        }
      })
    },
    createRole () {
      this.formData.permissions = ['Pages.Users', 'Pages.Roles', 'Pages.Tenants']
      Create(this.formData).then(res => {
        this.loading = false
        if (res.success) {
          this.handleClose()
          this.$notify({
            title: '提示',
            message: '添加成功',
            type: 'success'

          })
          this.loading = false
        }
      })
    },
    updateRole () {
      Update(this.formData).then(res => {
        this.loading = false
        if (res.success) {
          this.handleClose()
          this.$notify({
            title: '提示',
            message: '修改成功',
            type: 'success'

          })
          this.loading = false
        }
      })
    },
    show (value, title) {
      this.dialogVisible = true
      if (value !== '') {
        this.formData = value
      }
      this.title = title
    },
    handleClose () {
      this.dialogVisible = false
      this.$emit('fromvisible', false)
    }
  }
}
</script>
